﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Siser.Web.Models;
using Siser.Web.Models.JsonResponses;
using Siser.Web.Shared;

namespace Siser.Web.Controllers
{ 
    public class PlantaController : Controller
    {
        private SiserEntities db = new SiserEntities();

        public ViewResult Index()
        {
            var plantas = db.Plantas.Include(p => p.Empresa).Include(p => p.Region);
            return View(plantas.ToList());
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #region "   CRUD"

        public ActionResult Create()
        {
            ViewBag.EmpresaId = new SelectList(db.Empresas, "Id", "Nombre");
            ViewBag.RegionId = new SelectList(db.Regiones, "Id", "Nombre");
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Planta planta)
        {
            if (ModelState.IsValid)
            {
                db.Plantas.Add(planta);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.EmpresaId = new SelectList(db.Empresas, "Id", "Nombre", planta.EmpresaId);
            ViewBag.RegionId = new SelectList(db.Regiones, "Id", "Nombre", planta.RegionId);
            return View(planta);
        }
        
        public ActionResult Edit(int id)
        {
            Planta planta = db.Plantas.Find(id);
            ViewBag.EmpresaId = new SelectList(db.Empresas, "Id", "Nombre", planta.EmpresaId);
            ViewBag.RegionId = new SelectList(db.Regiones, "Id", "Nombre", planta.RegionId);
            return View(planta);
        }

        [HttpPost]
        public ActionResult Edit(Planta planta)
        {
            if (ModelState.IsValid)
            {
                db.Entry(planta).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.EmpresaId = new SelectList(db.Empresas, "Id", "Nombre", planta.EmpresaId);
            ViewBag.RegionId = new SelectList(db.Regiones, "Id", "Nombre", planta.RegionId);
            return View(planta);
        }

        public ActionResult Delete(int id)
        {
            Planta planta = db.Plantas.Find(id);
            return View(planta);
        }

        public ViewResult Details(int id)
        {
            Planta planta = db.Plantas.Find(id);
            return View(planta);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Planta planta = db.Plantas.Find(id);
            db.Plantas.Remove(planta);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        #endregion

        [HttpPost]
        public JsonResult GetPlantas()
        {
            var plantasJson = (from p in db.Plantas
                                join x in
                                    (from e in db.Equipos
                                     join ue in db.UsuariosEquipos on e.Id equals ue.Equipo.Id
                                     join u in db.Usuarios on ue.Usuario.Id equals u.Id
                                     join i in db.Instalaciones on e.Instalacion.Id equals i.Id
                                     where e.Vigencia == true
                                        && u.Vigencia == true
                                        && u.Nombre == HttpContext.User.Identity.Name
                                     select i) on p.Id equals x.PlantaId
                                select new JsonDataMap()
                                {
                                    Id = p.Id,
                                    Latitud = p.Latitud,
                                    Longitud = p.Longitud,
                                    Nombre = p.Nombre,
                                    Tipo = "PLA",
                                    Controller = "",
                                    Action = "",
                                    Parametro = 0,
                                    NivelAlarma = ""
                                }).Distinct().ToList();

            for (int i = 0; i < plantasJson.Count; i++)
            {
                plantasJson[i].NivelAlarma = SystemAlarm.GetAlarmLevelByPlanta(HttpContext.User.Identity.Name, plantasJson[i].Id);
                plantasJson[i].Id = null;
            }

            return Json(plantasJson);
        }

        [ActionName("PlantaPorEmpresa")]
        public JsonResult getPlantaPorEmpresa(string empresa)
        {
            var plantasJSON = (
                                    from p in db.Plantas
                                    join emp in db.Empresas on p.EmpresaId equals emp.Id
                                    select p.Nombre
                                    //where p.Empresa = empresa         
                              ).ToList();
            return Json(plantasJSON);
        }
    }
}